package com.xquant.transaction_management_system.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xquant.transaction_management_system.model.PO.FinancialInstruments;
import com.xquant.transaction_management_system.model.VO.SearchVo;
import com.xquant.transaction_management_system.service.FinancialInstrumentsService;
import com.xquant.transaction_management_system.utils.Result;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api("金融工具服务控制层")
@RestController
@CrossOrigin
@RequestMapping("/admin")
public class FinancialInstrumentsController {
    @Autowired
    private FinancialInstrumentsService financialInstrumentsService;
    @GetMapping("/getDebenture/{page}/{limit}")
    public Result getDebenture(@PathVariable long page,
            @PathVariable long limit)
            {
        Page<FinancialInstruments> pageParam = new Page<>();
        pageParam.setCurrent(page-1);
        pageParam.setSize(limit);
        IPage<FinancialInstruments> pageModel = financialInstrumentsService.selectPage(pageParam);
        return Result.ok(pageModel);
    }

    @GetMapping("/getAllDebentureByCode/{page}/{limit}/{debentureCode}")
    public Result getDebentureByCode(@PathVariable long page,
                                     @PathVariable long limit,
                                     @PathVariable String debentureCode)
    {
        Page<FinancialInstruments> pageParam = new Page<>();
        pageParam.setCurrent(page-1);
        pageParam.setSize(limit);
        System.out.println(debentureCode);
        if (debentureCode==null){
            System.out.println(debentureCode);
            IPage<FinancialInstruments> pageModel = financialInstrumentsService.selectPage(pageParam);
            return Result.ok(pageModel);
        }
        IPage<FinancialInstruments> pageModel = financialInstrumentsService.selectPageByDebentureCode(pageParam,debentureCode);
        return Result.ok(pageModel);
    }

    @PostMapping("/saveDebenture")
    public Result saveDebenture(@RequestBody FinancialInstruments financialInstruments){
        boolean flag = true;
        if (financialInstruments.getId()!=null){
            flag = financialInstrumentsService.updateById(financialInstruments);
        }else {
            flag = financialInstrumentsService.save(financialInstruments);
        }
        if (flag){
            return Result.ok();
        }
        return Result.fail();
    }

    @GetMapping("/select/{id}/{configurationId}")
    public Result select(@PathVariable Long id,
                         @PathVariable Long configurationId){
        FinancialInstruments serviceById = financialInstrumentsService.getById(id);
        serviceById.setCongigurationId(configurationId);
        financialInstrumentsService.updateById(serviceById);
        return Result.ok();
    }

    @GetMapping("/find/{id}")
    public Result find(@PathVariable Long id){
        FinancialInstruments serviceById = financialInstrumentsService.getById(id);
        return Result.ok(serviceById);
    }

    @GetMapping("/findByAccount/{page}/{limit}/{transactionAccount}")
    public Result findByAccount(@PathVariable Long page,
                                @PathVariable Long limit,
            @PathVariable String transactionAccount){
        Page<FinancialInstruments> pageParam = new Page<>();
        pageParam.setCurrent(page-1);
        pageParam.setSize(limit);
        QueryWrapper<FinancialInstruments> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("transaction_account",transactionAccount);
        IPage<FinancialInstruments> pageModel = financialInstrumentsService.getByAccountPage(pageParam,transactionAccount);
        return Result.ok(pageModel);
    }
    @GetMapping("/getDebentureByCodeAndAccount/{current}/{limit}/{debentureCode}/{account}")
    public Result getDebentureByCodeAndAccount(@PathVariable Long current,
                                @PathVariable Long limit, @PathVariable String debentureCode,
                                @PathVariable String account){
        Page<FinancialInstruments> pageParam = new Page<>();
        pageParam.setCurrent(current-1);
        pageParam.setSize(limit);
        QueryWrapper<FinancialInstruments> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("transaction_account",account);
        queryWrapper.like("debenture_code",debentureCode);
        IPage<FinancialInstruments> pageModel = financialInstrumentsService.getByAccountAndCodePage(pageParam,queryWrapper);
        return Result.ok(pageModel);
    }

    @GetMapping("/deleteOne/{id}")
    public Result deleteOne(@PathVariable Long id){
        financialInstrumentsService.removeById(id);
        return Result.ok();
    }

}
